package com.inossem.loadandrefresh.pullableview;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class PullableRelativeLayout extends RelativeLayout implements Pullable {

    boolean mPullDownFlag = true;

    boolean mPullUpFlag = true;

    ListView mListView;

    public PullableRelativeLayout(Context context) {
        super(context);
    }

    public PullableRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PullableRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setPullDownFlag(boolean pullDownFlag) {
        mPullDownFlag = pullDownFlag;
    }

    @Override
    public void setPullUpFlag(boolean pullUpFlag) {
        mPullUpFlag = pullUpFlag;
    }

    public void setListView(ListView listView) {
        this.mListView = listView;
    }

    @Override
    public boolean canPullDown() {
        if (!mPullDownFlag) {
            return false;
        }

        if (mListView.getCount() == 0) {
            // 没有item的时候也可以下拉刷新
            return true;
        } else if (mListView.getFirstVisiblePosition() == 0
                && mListView.getChildAt(0) != null && mListView.getChildAt(0).getTop() >= 0) {
            // 滑到ListView的顶部了
            return true;
        } else
            return false;
    }

    @Override
    public boolean canPullUp() {
        if (!mPullUpFlag) {
            return false;
        }
        return true;
    }

}
